package com.jerry.template.module.welcome.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;

import com.jerry.template.R;
import com.jerry.template.base.BaseActivity;
import com.jerry.template.module.home.activity.HomeActivity;
import com.jerry.template.module.welcome.adapter.WelcomeAdapter;

import butterknife.BindView;

/**
 * Created by chenhuan on 2016/5/10.
 */
public class WelcomeActivity extends BaseActivity{
    int[] imageId = {R.drawable.bg_intros1,R.drawable.bg_intros2,R.drawable.bg_intros3};

    @BindView(R.id.welcome_vp)
    ViewPager mVp;

    private boolean isFirstRun = true;
    private WelcomeAdapter adapter;

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            Intent intent = new Intent(WelcomeActivity.this, HomeActivity.class);
            startActivity(intent);
            finish();
        }
    };

    private void goToHome(){
        handler.sendEmptyMessageDelayed(1,3000);
    }

    @Override
    protected int getLayoutId(){
        return R.layout.activity_welcome;
    }

    @Override
    protected void loadData(){
        adapter = new WelcomeAdapter(this,imageId);
        mVp.setAdapter(adapter);

    }

    @Override
    protected void initViews(){
        isFirstRun = isFirstRun();

        if(isFirstRun){
            loadData();
        }else{
            setContentView(R.layout.welcome_image);
            goToHome();
        }
    }

    private boolean isFirstRun(){
        SharedPreferences sp = getSharedPreferences("welcome",MODE_PRIVATE);
        return sp.getBoolean("isFirstRun",true);
    }
}
